Iterators

The macros agg_iterate(s, e) and agg_iterate_end(s, e) implement iterators for arbitrary collections. The macros must surround the body over which the loop iterates. s and e denote the collection and the element, respectively. They must both be declared with the correct types. The actual expression for e must be a variable, optionally preceded with its type; the actual expression for s should be a variable, since it might be evaluated more than once.

The following example calls the function f for all elements of a collection c:

C c = ...;

agg_iterate (c, E e)
{ 
   f (e);
}
agg_iterate_end (c, e);

For iterating over a collection in the reverse direction a pair of similar iterators named agg_iterate_reverse[_end](s, e) are provided.

Furthermore, the macros agg_iterate_association[_end](s, key, info) implement iterators over associations (in particular over mappings).

Finally, the macros agg_iterate_double[_end](s1, e1, s2, e2, comp) implement iterators over two collections, where comp is an integer variable, which is set to -1, 1, or 0, depending on whether only the first, second, or both collections are exhausted when the iteration terminates. The following example implements a ``lexicographic'' comparison of the two collections s1 and s2 assuming a type Ordered for which an operator < is defined:

int compare (Ordered_Collection s1, Ordered_Collection s2)
{ 
   int result;

   agg_iterate_double (s1, Ordered e1, s2, Ordered e2, result)
   {  
      if (e1 < e2) return -1;
      else if (e2 < e1) return 1;
   }
   agg_iterate_double_end (s1, e1, s2, e2, result);

   return result;
}